using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Gospel.DB
{
	/// <summary>
	/// Strongly-typed collection for the Article class.
	/// </summary>
	[Serializable]
	public partial class ArticleCollection : ActiveList<Article, ArticleCollection> 
	{	   
		public ArticleCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the vbsa_articles table.
	/// </summary>
	[Serializable]
	public partial class Article : ActiveRecord<Article>
	{
		#region .ctors and Default Settings
		
		public Article()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Article(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Article(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Article(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("vbsa_articles", TableType.Table, DataService.GetInstance("VBSA"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarArticleId = new TableSchema.TableColumn(schema);
				colvarArticleId.ColumnName = "article_id";
				colvarArticleId.DataType = DbType.Int32;
				colvarArticleId.MaxLength = 0;
				colvarArticleId.AutoIncrement = true;
				colvarArticleId.IsNullable = false;
				colvarArticleId.IsPrimaryKey = true;
				colvarArticleId.IsForeignKey = false;
				colvarArticleId.IsReadOnly = false;
				colvarArticleId.DefaultSetting = @"";
				colvarArticleId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleId);
				
				TableSchema.TableColumn colvarArticleTitleCn = new TableSchema.TableColumn(schema);
				colvarArticleTitleCn.ColumnName = "article_title_cn";
				colvarArticleTitleCn.DataType = DbType.String;
				colvarArticleTitleCn.MaxLength = 50;
				colvarArticleTitleCn.AutoIncrement = false;
				colvarArticleTitleCn.IsNullable = false;
				colvarArticleTitleCn.IsPrimaryKey = false;
				colvarArticleTitleCn.IsForeignKey = false;
				colvarArticleTitleCn.IsReadOnly = false;
				colvarArticleTitleCn.DefaultSetting = @"";
				colvarArticleTitleCn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleTitleCn);
				
				TableSchema.TableColumn colvarArticleTitleTw = new TableSchema.TableColumn(schema);
				colvarArticleTitleTw.ColumnName = "article_title_tw";
				colvarArticleTitleTw.DataType = DbType.String;
				colvarArticleTitleTw.MaxLength = 50;
				colvarArticleTitleTw.AutoIncrement = false;
				colvarArticleTitleTw.IsNullable = false;
				colvarArticleTitleTw.IsPrimaryKey = false;
				colvarArticleTitleTw.IsForeignKey = false;
				colvarArticleTitleTw.IsReadOnly = false;
				colvarArticleTitleTw.DefaultSetting = @"";
				colvarArticleTitleTw.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleTitleTw);
				
				TableSchema.TableColumn colvarArticleTagsCn = new TableSchema.TableColumn(schema);
				colvarArticleTagsCn.ColumnName = "article_tags_cn";
				colvarArticleTagsCn.DataType = DbType.String;
				colvarArticleTagsCn.MaxLength = 300;
				colvarArticleTagsCn.AutoIncrement = false;
				colvarArticleTagsCn.IsNullable = true;
				colvarArticleTagsCn.IsPrimaryKey = false;
				colvarArticleTagsCn.IsForeignKey = false;
				colvarArticleTagsCn.IsReadOnly = false;
				colvarArticleTagsCn.DefaultSetting = @"";
				colvarArticleTagsCn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleTagsCn);
				
				TableSchema.TableColumn colvarArticleTagsTw = new TableSchema.TableColumn(schema);
				colvarArticleTagsTw.ColumnName = "article_tags_tw";
				colvarArticleTagsTw.DataType = DbType.String;
				colvarArticleTagsTw.MaxLength = 300;
				colvarArticleTagsTw.AutoIncrement = false;
				colvarArticleTagsTw.IsNullable = true;
				colvarArticleTagsTw.IsPrimaryKey = false;
				colvarArticleTagsTw.IsForeignKey = false;
				colvarArticleTagsTw.IsReadOnly = false;
				colvarArticleTagsTw.DefaultSetting = @"";
				colvarArticleTagsTw.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleTagsTw);
				
				TableSchema.TableColumn colvarFatherId = new TableSchema.TableColumn(schema);
				colvarFatherId.ColumnName = "father_id";
				colvarFatherId.DataType = DbType.Int32;
				colvarFatherId.MaxLength = 0;
				colvarFatherId.AutoIncrement = false;
				colvarFatherId.IsNullable = false;
				colvarFatherId.IsPrimaryKey = false;
				colvarFatherId.IsForeignKey = false;
				colvarFatherId.IsReadOnly = false;
				colvarFatherId.DefaultSetting = @"";
				colvarFatherId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFatherId);
				
				TableSchema.TableColumn colvarArticleContentCn = new TableSchema.TableColumn(schema);
				colvarArticleContentCn.ColumnName = "article_content_cn";
				colvarArticleContentCn.DataType = DbType.String;
				colvarArticleContentCn.MaxLength = 1073741823;
				colvarArticleContentCn.AutoIncrement = false;
				colvarArticleContentCn.IsNullable = true;
				colvarArticleContentCn.IsPrimaryKey = false;
				colvarArticleContentCn.IsForeignKey = false;
				colvarArticleContentCn.IsReadOnly = false;
				colvarArticleContentCn.DefaultSetting = @"";
				colvarArticleContentCn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleContentCn);
				
				TableSchema.TableColumn colvarArticleContentTw = new TableSchema.TableColumn(schema);
				colvarArticleContentTw.ColumnName = "article_content_tw";
				colvarArticleContentTw.DataType = DbType.String;
				colvarArticleContentTw.MaxLength = 1073741823;
				colvarArticleContentTw.AutoIncrement = false;
				colvarArticleContentTw.IsNullable = true;
				colvarArticleContentTw.IsPrimaryKey = false;
				colvarArticleContentTw.IsForeignKey = false;
				colvarArticleContentTw.IsReadOnly = false;
				colvarArticleContentTw.DefaultSetting = @"";
				colvarArticleContentTw.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArticleContentTw);
				
				TableSchema.TableColumn colvarCreated = new TableSchema.TableColumn(schema);
				colvarCreated.ColumnName = "created";
				colvarCreated.DataType = DbType.DateTime;
				colvarCreated.MaxLength = 0;
				colvarCreated.AutoIncrement = false;
				colvarCreated.IsNullable = false;
				colvarCreated.IsPrimaryKey = false;
				colvarCreated.IsForeignKey = false;
				colvarCreated.IsReadOnly = false;
				
						colvarCreated.DefaultSetting = @"(getdate())";
				colvarCreated.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreated);
				
				TableSchema.TableColumn colvarPersonId = new TableSchema.TableColumn(schema);
				colvarPersonId.ColumnName = "person_id";
				colvarPersonId.DataType = DbType.Int32;
				colvarPersonId.MaxLength = 0;
				colvarPersonId.AutoIncrement = false;
				colvarPersonId.IsNullable = false;
				colvarPersonId.IsPrimaryKey = false;
				colvarPersonId.IsForeignKey = true;
				colvarPersonId.IsReadOnly = false;
				colvarPersonId.DefaultSetting = @"";
				
					colvarPersonId.ForeignKeyTableName = "vbsa_users";
				schema.Columns.Add(colvarPersonId);
				
				TableSchema.TableColumn colvarActive = new TableSchema.TableColumn(schema);
				colvarActive.ColumnName = "active";
				colvarActive.DataType = DbType.Boolean;
				colvarActive.MaxLength = 0;
				colvarActive.AutoIncrement = false;
				colvarActive.IsNullable = false;
				colvarActive.IsPrimaryKey = false;
				colvarActive.IsForeignKey = false;
				colvarActive.IsReadOnly = false;
				
						colvarActive.DefaultSetting = @"((1))";
				colvarActive.ForeignKeyTableName = "";
				schema.Columns.Add(colvarActive);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["VBSA"].AddSchema("vbsa_articles",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("ArticleId")]
		public int ArticleId 
		{
			get { return GetColumnValue<int>("article_id"); }

			set { SetColumnValue("article_id", value); }

		}

		  
		[XmlAttribute("ArticleTitleCn")]
		public string ArticleTitleCn 
		{
			get { return GetColumnValue<string>("article_title_cn"); }

			set { SetColumnValue("article_title_cn", value); }

		}

		  
		[XmlAttribute("ArticleTitleTw")]
		public string ArticleTitleTw 
		{
			get { return GetColumnValue<string>("article_title_tw"); }

			set { SetColumnValue("article_title_tw", value); }

		}

		  
		[XmlAttribute("ArticleTagsCn")]
		public string ArticleTagsCn 
		{
			get { return GetColumnValue<string>("article_tags_cn"); }

			set { SetColumnValue("article_tags_cn", value); }

		}

		  
		[XmlAttribute("ArticleTagsTw")]
		public string ArticleTagsTw 
		{
			get { return GetColumnValue<string>("article_tags_tw"); }

			set { SetColumnValue("article_tags_tw", value); }

		}

		  
		[XmlAttribute("FatherId")]
		public int FatherId 
		{
			get { return GetColumnValue<int>("father_id"); }

			set { SetColumnValue("father_id", value); }

		}

		  
		[XmlAttribute("ArticleContentCn")]
		public string ArticleContentCn 
		{
			get { return GetColumnValue<string>("article_content_cn"); }

			set { SetColumnValue("article_content_cn", value); }

		}

		  
		[XmlAttribute("ArticleContentTw")]
		public string ArticleContentTw 
		{
			get { return GetColumnValue<string>("article_content_tw"); }

			set { SetColumnValue("article_content_tw", value); }

		}

		  
		[XmlAttribute("Created")]
		public DateTime Created 
		{
			get { return GetColumnValue<DateTime>("created"); }

			set { SetColumnValue("created", value); }

		}

		  
		[XmlAttribute("PersonId")]
		public int PersonId 
		{
			get { return GetColumnValue<int>("person_id"); }

			set { SetColumnValue("person_id", value); }

		}

		  
		[XmlAttribute("Active")]
		public bool Active 
		{
			get { return GetColumnValue<bool>("active"); }

			set { SetColumnValue("active", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Gospel.DB.ArticleAttachmentCollection ArticleAttachments()
		{
			return new Gospel.DB.ArticleAttachmentCollection().Where(ArticleAttachment.Columns.ArticleId, ArticleId).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this Article
		/// 
		/// </summary>
		public Gospel.DB.User User
		{
			get { return Gospel.DB.User.FetchByID(this.PersonId); }

			set { SetColumnValue("person_id", value.PersonId); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varArticleTitleCn,string varArticleTitleTw,string varArticleTagsCn,string varArticleTagsTw,int varFatherId,string varArticleContentCn,string varArticleContentTw,DateTime varCreated,int varPersonId,bool varActive)
		{
			Article item = new Article();
			
			item.ArticleTitleCn = varArticleTitleCn;
			
			item.ArticleTitleTw = varArticleTitleTw;
			
			item.ArticleTagsCn = varArticleTagsCn;
			
			item.ArticleTagsTw = varArticleTagsTw;
			
			item.FatherId = varFatherId;
			
			item.ArticleContentCn = varArticleContentCn;
			
			item.ArticleContentTw = varArticleContentTw;
			
			item.Created = varCreated;
			
			item.PersonId = varPersonId;
			
			item.Active = varActive;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varArticleId,string varArticleTitleCn,string varArticleTitleTw,string varArticleTagsCn,string varArticleTagsTw,int varFatherId,string varArticleContentCn,string varArticleContentTw,DateTime varCreated,int varPersonId,bool varActive)
		{
			Article item = new Article();
			
				item.ArticleId = varArticleId;
				
				item.ArticleTitleCn = varArticleTitleCn;
				
				item.ArticleTitleTw = varArticleTitleTw;
				
				item.ArticleTagsCn = varArticleTagsCn;
				
				item.ArticleTagsTw = varArticleTagsTw;
				
				item.FatherId = varFatherId;
				
				item.ArticleContentCn = varArticleContentCn;
				
				item.ArticleContentTw = varArticleContentTw;
				
				item.Created = varCreated;
				
				item.PersonId = varPersonId;
				
				item.Active = varActive;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string ArticleId = @"article_id";
			 public static string ArticleTitleCn = @"article_title_cn";
			 public static string ArticleTitleTw = @"article_title_tw";
			 public static string ArticleTagsCn = @"article_tags_cn";
			 public static string ArticleTagsTw = @"article_tags_tw";
			 public static string FatherId = @"father_id";
			 public static string ArticleContentCn = @"article_content_cn";
			 public static string ArticleContentTw = @"article_content_tw";
			 public static string Created = @"created";
			 public static string PersonId = @"person_id";
			 public static string Active = @"active";
						
		}

		#endregion
	}

}

